home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1270 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  90 lines

  1. Path: gustav.unda.fi!olle
  2. From: olle@gustav.unda.fi (Olavi Sakari)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Splitting String ?
  5. Date: 12 Jan 1996 17:55:35 GMT
  6. Organization: Unda Oy, a Scitex company
  7. Distribution: world
  8. Message-ID: <4d67an$303@unda.fi>
  9. References: <HAKOLA.96Jan12151128@jung.hut.fi>
  10. NNTP-Posting-Host: gustav.unda.fi
  11.  
  12. In article <HAKOLA.96Jan12151128@jung.hut.fi>, hakola@cadmail.hut.fi (Petri Hakola) writes:
  13.  > 
  14.  >     Have I missed something (again:)
  15.  
  16. Yes...
  17.  
  18.  >                                      or why doesn't this code
  19.  >     work? I should split dos-a-like-filename and add new postfix
  20.  >     instead of old one (i.e. DATA.TXT --> DATA.UPD  It seems to
  21.  >     work correctly if the filename has an old postfix, but if
  22.  >     there isn't one start won't return what it should.
  23.  > 
  24.  >                             - P -
  25.  > 
  26.  > 
  27.  > ---Clip---
  28.  > #include <stdio.h>
  29.  > #include <string.h>
  30.  > 
  31.  > char *newname(char *s) {
  32.  > 
  33.  >    char *dot;
  34.  >    char *start;
  35.  >    char end[5];
  36.  > 
  37.  >    strcpy(end,".UPD");
  38.  
  39. Why not replace three previous lines by e.g.
  40.  
  41.       static char *end = ".UPD";
  42.  
  43.  >    start = s;
  44.  >    dot = strchr(s,'.');
  45.  >    if( dot == NULL) {
  46.  >      start[strlen(start)] = '\0';
  47.  >      dot = end;
  48.  
  49. Previous line not needed.
  50.  
  51.  >    } else {
  52.  >    *dot = '\0';
  53.  >    dot++;
  54.  >    dot = end;
  55.  
  56. Previous two lines not needed.
  57.  
  58.  >    }
  59.  >    strcat(start, dot);
  60.  
  61. Use
  62.  
  63.       strcat(start, end);
  64.  
  65. instead!
  66.  
  67.  >    return start;
  68.  > }
  69.  > 
  70.  > main() {
  71.  >   printf("%s\n",newname("LONGNAME.TXT"));
  72.  >   printf("%s\n",newname("NOEND"));
  73.  > }
  74.  
  75. (In the prevous code the variable start is not really needed, but wait...)
  76.  
  77. You'll need to e.g. malloc() in newname for a string with enough room for the
  78. original string (w/o the extension) plus end (plus '\0') and copy the input
  79. string / head of the input string there and _then_ strcat on that!
  80.  
  81. (You cannot simply strcat on "NOEND" and I don't think that it is a great idea
  82. to modify "LONGNAME.TXT", either, even if it just happens to work in your
  83. environment, and even if this is just an example of using newname!)
  84.  
  85. Olle
  86. --
  87.   Olavi Sakari                          osakari@unda.fi
  88.   +358 0 5255 8556 (voice)              +358 0 5255 8585 (fax)
  89.   Check out http://www.unda.fi/
  90.